import type { IHttpContext, IMiddleware, TNext } from "boot/interface"

export class ResponseMiddleware implements IMiddleware {
  async use(
    ctx: IHttpContext,
    next: TNext
  ) {
    // 执行前面所有的中间件
    const data = await next() || await ctx.body
    if (typeof data === 'object') {
      ctx.body = {
        code: 0,
        ...data
      }
    } else {
      ctx.body = {
        code: 0,
        data
      }
    }
  }
}
